<?php 
// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

if(isset($_GET['LATITUDE']) && isset($_GET['LONGITUDE'])){
	$latitude = $_GET['LATITUDE'];
	$longitude = $_GET['LONGITUDE'];
	
	// Search nearby
	$radius = 10; // kilometers
	mysql_query("set names 'utf8'");
	$sql = mysql_query("SELECT id, TEN_DIA_DIEM, ( 6371 * acos( cos( radians( '$latitude' ) ) *
			cos( radians( LATITUDE ) ) * cos( radians( LONGITUDE ) - radians('$longitude') ) +
			sin( radians( $latitude ) ) * sin( radians( LATITUDE ) ) ) )
			AS distance FROM MON_AN HAVING distance < '$radius' ORDER BY distance")
			or die("error");
	$nearby_check = mysql_num_rows($sql);
	if($nearby_check > 0){
		$response["places_food"] = array();
		while ($row = mysql_fetch_array($sql)){
			$places = array();
			
			$places["id"] = $row['id'];
			$places["TEN_DIA_DIEM"] = $row["TEN_DIA_DIEM"];
			//$place["distance"] = $row["distance"];
			array_push($response["places_food"], $places);
		}
		// success
		$response["success"] = 1;
		// echoing JSON response
		echo utf8_encode(json_encode($response));
	} else {
	    // no products found
	    $response["success"] = 0;
	    $response["message"] = "No foods found";
	 
	    // echo no users JSON
	    print utf8_encode(json_encode($response));
	}
	
} else {
    // required field is missing
    $response["success"] = -1;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
} 
?>